package com.xhwl.data.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhwl.common.enums.centsecurity.TaskStatusEnum;
import com.xhwl.common.enums.decorate.DecorateStatusEnum;
import com.xhwl.common.enums.goodsrelease.GoodsReleaseStatusEnum;
import com.xhwl.common.enums.ifm.insp.InspOrderStatusEnum;
import com.xhwl.common.enums.ifm.pm.OrderStatusEnum;
import com.xhwl.common.enums.order.MatterStatusEnum;
import com.xhwl.common.enums.order.check.CheckTaskStatusEnum;
import com.xhwl.common.enums.order.check.CheckTaskStatusEnumV1;
import com.xhwl.common.enums.visitor.VisitorStatusEnum;
import com.xhwl.common.enums.workbench.BlockTypeEnum;
import com.xhwl.common.enums.workbench.MyTaskStatusEnum;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.Account;
import com.xhwl.common.pojo.workbench.BlockCount;
import com.xhwl.common.pojo.workbench.MyTask;
import com.xhwl.common.query.workbanch.MyTaskQuery;
import com.xhwl.common.utils.DateUtils;
import com.xhwl.data.service.IAccountTaskService;
import com.xhwl.data.service.IMyTaskService;
import lombok.Data;
import lombok.extern.java.Log;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: luyi
 * @Description: 我的代办
 * @Date: Create in 10:29 2021/8/11
 */
@RestController
@Log
public class MyTaskController {

	@Autowired
	private IMyTaskService myTaskService;

	@Autowired
	private IAccountTaskService accountTaskService;


	@RequestMapping("/my-task/page")
	public ResultJson page(@RequestBody MyTaskQueryPage myTaskQueryPage, Account account){
		MyTaskQuery myTaskQuery = myTaskQueryPage.getMyTaskQuery();
		myTaskQuery.setAccountId(Arrays.asList(account.getId()));
		Date threeMonthBefore = DateUtils.nextMonth(-3);
		if(null != myTaskQueryPage.getMyTaskQuery().getStart()){
			if(myTaskQueryPage.getMyTaskQuery().getStart().getTime() < threeMonthBefore.getTime()){
				myTaskQueryPage.getMyTaskQuery().setStart(threeMonthBefore);
			}
		}else{
			myTaskQueryPage.getMyTaskQuery().setStart(threeMonthBefore);
		}
		Page<MyTask> page1 = myTaskService.page(myTaskQuery, myTaskQueryPage.getPage());
		return ResultJson.success(toDTO(page1));
	}

	private Page toDTO(Page page1) {
		List<MyTask> records = page1.getRecords();
		if(CollectionUtils.isEmpty(records)){
			return page1;
		}
		List<MyTaskDTO> myTaskDTOS = new ArrayList<>(records.size());
		List<Integer> collect = records.stream().map(i -> i.getId()).collect(Collectors.toList());
		// 查询当前执行人
		Map<Integer, String> accountNameByTaskId = accountTaskService.findAccountNameByTaskId(collect, MyTaskStatusEnum.TODO);
		for (MyTask record : records) {
			MyTaskDTO myTaskDTO = new MyTaskDTO();
			BeanUtils.copyProperties(record, myTaskDTO);
			Map<Integer, String> map = getTypeInfo(myTaskDTO.getBlockType());
			myTaskDTO.setBlockStatusCN(map.get(record.getBlockStatus()));
			myTaskDTO.setOperator(accountNameByTaskId.get(record.getId()));
			myTaskDTOS.add(myTaskDTO);
		}
		return page1.setRecords(myTaskDTOS);
	}

	@RequestMapping("/my-task/count-block-type")
	public ResultJson countBlockType(@RequestBody MyTaskQuery myTaskQuery, Account account){
		if(null == myTaskQuery.getProjectId()){
			return ResultJson.fail("缺少项目信息");
		}
		Date threeMonthBefore = DateUtils.nextMonth(-3);
		if(null != myTaskQuery.getStart()){
			if(myTaskQuery.getStart().getTime() < threeMonthBefore.getTime()){
				myTaskQuery.setStart(threeMonthBefore);
			}
		}else{
			myTaskQuery.setStart(threeMonthBefore);
		}
		myTaskQuery.setAccountId(Arrays.asList(account.getId()));
		List<BlockCount> result = myTaskService.countBlockType(myTaskQuery);
		return ResultJson.success(result);
	}

	@RequestMapping("/my-task/type")
	public ResultJson type(BlockTypeEnum type){
		Map<Integer, String> typeInfo = getTypeInfo(type);
		List<Map<Object, Object>> result = new ArrayList<>();
		for(Map.Entry entry : typeInfo.entrySet()){
			Map<Object, Object> map = new HashMap<>();
			map.put("k", entry.getKey());
			map.put("v", entry.getValue());
			result.add(map);
		}
		return ResultJson.success(result);
	}

	private Map<Integer, String> getTypeInfo(BlockTypeEnum type){
		Map<Integer, String> map = new HashMap<>();
		if(BlockTypeEnum.ORDER_MANAGER == type){
			List<MatterStatusEnum> toDo = MatterStatusEnum.getToDo();
			for (MatterStatusEnum value : toDo) {
				map.put(value.getStatus(), value.getDesc());
			}
		}else if(BlockTypeEnum.INSPECTION_TASK == type || BlockTypeEnum.VIDEO_TASK == type){
			// 巡视巡检
			for (TaskStatusEnum value : TaskStatusEnum.values()) {
				if(1 == value.getIsShow()){
					Short aShort = Short.valueOf(value.getStatus());
					map.put(aShort.intValue(), value.description);
				}
			}
		}else if(BlockTypeEnum.DECORATION_TASK == type){
			for (DecorateStatusEnum value : DecorateStatusEnum.values()) {
				if(1 == value.getIsShow()){
					Short aShort = Short.valueOf(value.getStatus());
					map.put(aShort.intValue(), value.getDescription());
				}
			}
		}else if(BlockTypeEnum.ITEM_MANAGEMENT == type){
			for (GoodsReleaseStatusEnum value : GoodsReleaseStatusEnum.values()) {
				map.put(value.getKey(), value.getDisplay());
			}
		}else if(BlockTypeEnum.CHECK_TASK == type){
			for (CheckTaskStatusEnumV1 value : CheckTaskStatusEnumV1.values()) {
				map.put(value.getType(), value.getDesc());
			}
		}else if(BlockTypeEnum.MAINTAIN == type){
			for (OrderStatusEnum value : OrderStatusEnum.values()) {
				map.put(value.getCode(), value.getName());
			}
		}else if(BlockTypeEnum.INSPECTION == type){
			for (InspOrderStatusEnum value : InspOrderStatusEnum.values()) {
				map.put(value.getCode(), value.getName());
			}
		}else if (BlockTypeEnum.VISITOR_APPOINTMENT == type){
			// 访客预约
			for (VisitorStatusEnum value : VisitorStatusEnum.values()){
				map.put(value.getType(), value.getDescription());
			}
		}
		return map;
	}

	@Data
	static class MyTaskQueryPage{

		private MyTaskQuery myTaskQuery;

		private Page page;
	}

	@Data
	class MyTaskDTO extends MyTask{

		private String blockStatusCN;

		private String operator;
	}

}

